Thread: [Jumping into C++] Chapter 7 Problem 1 Question

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    9

    [Jumping into C++] Chapter 7 Problem 1 Question

    Hi everyone, I'm new to this forum. I've been reading Jumping Into C++ and I love the book so far. My problem is that is directly jumps into hard mode with these practice problems. I have no idea how to do "1. Implement the source code that turns numbers into English text. " because I haven't learned it yet. I was hoping the book would be straight forward that you learn everything you need to complete the problem but I guess it doesn't seem the way. I haven't tried solving it because I have no idea what to do. I'm stumped, any help would be greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The world isn't full of problems where you know everything to solve the problem.

    Sooner or later, you have to start to really think about problems, break them down and solve problems in steps.

    For example, if the input number was restricted to say 1 to 10, could you make a go of it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I don't believe anyone answering questions here was involved in writing that book. In fact, most of us don't have a copy and have no idea what's in it so it's best not to assume that we are familiar with it.

    However, sneaking a cheeky peek at a copy it looks to me that he's given you more than enough information. In fact, he's pretty much told you how to do it.

    From the text:
    So our algorithm will look something like this:
    1) Break the number up into chunks of 3 digits
    2) For each three digit chunk, compute the text; append the magnitude of that chunk; append the chunks together
    3) To compute the text of a three digit chunk, compute the number of hundreds, and convert that one digit number to text, and add “hundreds”, appending the text of the two digit chunk
    4) To compute the text of a two digit chunk, if it’s less than 20, look it up; if it’s greater than 20, compute the number of tens, and look up the word, and append the text of the one digit number
    Where are you stuck?

  4. #4
    Registered User
    Join Date
    Jul 2017
    Posts
    9
    Quote Originally Posted by Salem View Post
    The world isn't full of problems where you know everything to solve the problem.

    Sooner or later, you have to start to really think about problems, break them down and solve problems in steps.

    For example, if the input number was restricted to say 1 to 10, could you make a go of it?
    Yeah because it's restricted. I'm thinking of if you type any number at all it will translate it to you in English. But you have a point I guess I should just try. Thank you for the response.

  5. #5
    Registered User
    Join Date
    Jul 2017
    Posts
    9
    Quote Originally Posted by algorism View Post
    I don't believe anyone answering questions here was involved in writing that book. In fact, most of us don't have a copy and have no idea what's in it so it's best not to assume that we are familiar with it.

    However, sneaking a cheeky peek at a copy it looks to me that he's given you more than enough information. In fact, he's pretty much told you how to do it.

    From the text:


    Where are you stuck?
    How to actually break down the 3 digit chunks because I haven't learned arrays yet (unless it doesn't require arrays).

  6. #6
    Registered User
    Join Date
    Jul 2017
    Posts
    9
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string tens(int number);
    
    int main()
    {
        cout << tens(3);
    }
    
    string tens(int number)
    {
        if (number == 0)
        {
            return "Zero";
        }
        else if (number == 1)
        {
            return "One";
        }
        else if (number == 2)
        {
            return "Two";
        }
        else if (number == 3)
        {
            return "Three";
        }
        else if (number == 4)
        {
            return "Four";
        }
        else if (number == 5)
        {
            return "Five";
        }
        else if (number == 6)
        {
            return "Six";
        }
        else if (number == 7)
        {
            return "Seven";
        }
        else if (number == 8)
        {
            return "Eight";
        }
        else if (number == 9)
        {
            return "Nine";
        }
    }

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    How to actually break down the 3 digit chunks
    That depends. Recursion would simplify the problem, but you may not know about that yet.

    Here's another (untested!) possibility:
    Code:
    if (n >= 1000000000) {
        chunk = n / 1000000000;
        n %= 1000000000;
    } else if (n >= 1000000)
        chunk = n / 1000000;
        n %= 1000000;
    } else if (n >= 1000)
        chunk = n / 1000;
        n %= 1000;
    } else {
        chunk = n;
        n = 0;
    }
    That gives you the highest 3 digit (at the most) chunk and reduces n so that the next time you will get the next highest chunk.
    If you need to go beyond billions (as you would for a 64-bit integer), then you'll need to extend it in the obvious way.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Good.

    Now study what this does
    int a = 42;
    cout << a / 10 << endl;
    cout << a % 10 << endl;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jul 2017
    Posts
    9
    Quote Originally Posted by Salem View Post
    Good.

    Now study what this does
    int a = 42;
    cout << a / 10 << endl;
    cout << a % 10 << endl;
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    string tens(int number);
    int main()
    {
        cout << "Please type your number you wish to convert: ";
        int number;
        cin >> number;
        if (number < 10)
        {
            cout << tens(number) << endl;
        }
        else if (number >= 10 && number < 100)
        {
            int first_digit, second_digit;
            first_digit = number / 10;
            second_digit = number % 10;
            cout << tens(first_digit) << " " << tens(second_digit) << endl;
        }
        return 0;
    }
    string tens(int number)
    {
        if (number == 0)
        {
            return "Zero";
        }
        else if (number == 1)
        {
            return "One";
        }
        else if (number == 2)
        {
            return "Two";
        }
        else if (number == 3)
        {
            return "Three";
        }
        else if (number == 4)
        {
            return "Four";
        }
        else if (number == 5)
        {
            return "Five";
        }
        else if (number == 6)
        {
            return "Six";
        }
        else if (number == 7)
        {
            return "Seven";
        }
        else if (number == 8)
        {
            return "Eight";
        }
        else if (number == 9)
        {
            return "Nine";
        }
    }
    I'm getting there but now I'm stuck on the "teen" part. I can see a lot of "if" statements incoming.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Another small clue that might simplify things.
    Code:
    string tens(int number) {
      char *words[] = {
        "Zero","One","Two","Three","Four",
        "Five","Six","Seven","Eight","Nine"
      };
      return words[number%10];
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Jul 2017
    Posts
    9
    Quote Originally Posted by Salem View Post
    Another small clue that might simplify things.
    Code:
    string tens(int number) {
      char *words[] = {
        "Zero","One","Two","Three","Four",
        "Five","Six","Seven","Eight","Nine"
      };
      return words[number%10];
    }

    stray '/240' in program error

  12. #12
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by LordAbolition View Post
    stray '/240' in program error
    The stray '\240' has nothing to do with Salem's code. You are obviously using the despicable windows OS. It has somehow converted at least some of the spaces to "non-breaking" spaces, presumably when you copied and pasted it. All I can say is to try again. You could also delete and retype all the spaces in that chunk of code.

  13. #13
    Registered User
    Join Date
    Jul 2017
    Posts
    9
    Quote Originally Posted by algorism View Post
    The stray '\240' has nothing to do with Salem's code. You are obviously using the despicable windows OS. It has somehow converted at least some of the spaces to "non-breaking" spaces, presumably when you copied and pasted it. All I can say is to try again. You could also delete and retype all the spaces in that chunk of code.
    You were right it was the spaces, I just had to reposition it more. It's odd that it does that but anyway back to work.

  14. #14
    Registered User
    Join Date
    Jul 2017
    Posts
    9
    Alright this is honestly the best I can do. It keeps crashing at the end and I don't know why.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    string tens(int number);
    string singles(int number);
    string bigger_tens(int number);
    int main()
    {
        /*cout << "Please type your number you wish to convert: ";
        int number;
        cin >> number; */
        for (int i=10; i < 30; i++)
        {
            if (i < 10)
            {
                cout << singles(i) << endl;
            }
            else if (i >= 10 && i < 20)
            {
                cout << tens(i) << endl;
            }
            else if (i >= 20 && i < 100)
            {
                cout << bigger_tens(i) << endl;
            }
        }
        return 0;
    }
    string singles(int number)
    {
        char *words[] =
        {
           "Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"
        };
        return words[number%10];
    }
    string tens(int number)
    {
        char *words[] =
        {
            "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"
        };
        return words[number%10];
    }
    string bigger_tens(int number)
    {
        char *words[] =
        {
            "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
        };
        string first_word = words[number%10];
        string second_word = singles(number/10);
        string whole_word = first_word + " " + second_word;
        return whole_word;
    }

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The program you posted doesn't even compile with my compiler so I can't duplicate your reported problem.
    main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
    main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
    main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
    main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
    main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
    main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
    main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
    main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
    main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
    main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
    Plus the other two places where you're doing similar operations.

    Why are you using C-strings, you seem to know about std::string so why not use the std::string instead. I would also recommend you consider using std::vector instead of the array and use the .at() function instead of the [] to access the vector.

    Something like:
    Code:
    string bigger_tens(int number)
    {
        std::vector<std::string> words{
            "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
        };
        string first_word = words.at(number%10);
        string second_word = singles(number/10);
        string whole_word = first_word + " " + second_word;
        return whole_word;
    }
    Then perhaps your system might give you more information as to what happened:
    terminate called after throwing an instance of 'std::out_of_range'
    what(): vector::_M_range_check: __n (which is 8) >= this->size() (which is 8)
    Aborted
    Another very good option would be for you to learn to use your debugger. The debugger will tell you exactly where the problem was detected and allow you to view the variables at the time of the crash.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 07-09-2016, 08:13 AM
  2. Question on "Jumping into C++" Chapter 13 practice problem 1
    By Ben Sturm in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2015, 01:16 PM
  3. Jumping in C++ Chapter 5 Question 2
    By etricity in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2014, 02:18 AM
  4. Jumping into C++, Chapter 5 Question 1.
    By etricity in forum C++ Programming
    Replies: 7
    Last Post: 04-02-2014, 06:55 AM
  5. Jumping into C++ chapter 8 question 5
    By twigz in forum C++ Programming
    Replies: 5
    Last Post: 03-07-2014, 04:43 PM

Tags for this Thread